package com.travel.lib.base.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by spc on 2017/8/10.
 */

public class ActivityManager {
    private static Stack<Activity> activityStack;
    private static ActivityManager instance;

    private ActivityManager() {
    }

    public static ActivityManager getActivityManager() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }


    /**
     * 压栈
     *
     * @param activity
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * pop除了mainactivity以外的
     */
    public void popMoreActivity() {
        if (activityStack == null)
            return;
        for (int i = 0; i < activityStack.size(); i++) {
            Activity act = activityStack.elementAt(i);
//            if (act.getClass().getName().equals(MainActivity.class.getName())) {
//                continue;
//            }
//            act.finish();
        }
    }


    /**
     * 结束指定activiy
     *
     * @param activity
     */
    public void popActivity(Activity activity) {
        if (activityStack == null)
            return;
        if (activity != null) {
            activityStack.remove(activity);
        }
    }


    /**
     * pop所有
     */
    public void popAllActivity() {
        if (activityStack == null)
            return;
        for (int i = 0; i < activityStack.size(); i++) {
            Activity act = activityStack.elementAt(i);
            act.finish();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivityByClass(Class<?> cls) {
        Activity activity = findActivity(cls);
        if (activity != null)
            activity.finish();
    }

    public Activity findActivity(Class<?> cls) {
        if (activityStack == null)
            return null;
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 获得activity的集合
     */
    public Stack<Activity> getActivityList() {
        return activityStack;
    }

}
